﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;

namespace SilveR
{
    public partial class DataPage : Page
    {
        public DataPage()
        {
            InitializeComponent();

            App app = (App)Application.Current;
            app.DataPage = this;
        }

        private Visibility SetTabVisible(bool visible)
        {
            if (visible)
                return System.Windows.Visibility.Visible;
            else
                return System.Windows.Visibility.Collapsed;
        }

        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            DataHandler dataHandler = new DataHandler();
            dataHandler.LoadFileCompleted += new DataHandler.LoadFileCompletedEventHandler(dataHandler_LoadFileCompleted);
            dataHandler.LoadFile(); //request and load the file
        }

        private void dataHandler_LoadFileCompleted(object sender, System.EventArgs e)
        {
            DataHandler dataHandler = (DataHandler)sender;

            if (dataHandler.SelectedFile != null) //so cancel wasn't pressed etc
            {
                DetermineFilenameSuffix(dataHandler); //determine the suffix for the filename

                //add in the data on a new tab
                TabItem newTabPage = new TabItem();
                newTabPage.Header = dataHandler.DisplayFileName;
                newTabPage.Content = new GridView(dataHandler);

                tabControlData.Items.Add(newTabPage);

                //add the new data to all the datahandlers...
                AnalysesPage analPage = ((App)Application.Current).AnalysesPage;
                if (analPage != null)
                {
                    foreach (TabItem tab in analPage.tabControlAnalyses.Items)
                    {
                        AnalysisView av = (AnalysisView)tab.Content;
                        av.Analysis.UpdateDataHandlers();
                    }
                }

                //show the last file that was loaded
                tabControlData.SelectedItem = newTabPage;
            }

            SetButtonAndTabStatus();
        }

        private void SetButtonAndTabStatus()
        {
            //set status of close button and tabcontrol
            tabControlData.Visibility = SetTabVisible(tabControlData.Items.Count > 0);
            btnClose.IsEnabled = (tabControlData.Items.Count > 0);
        }

        private void DetermineFilenameSuffix(DataHandler dataHandler)
        {
            //work out the display filename suffix

            restart: //use goto here as easiest way to get back to the start (could use recursion but would be overkill)
            foreach (TabItem tab in tabControlData.Items)
            {
                GridView gv = (GridView)tab.Content;
                if (dataHandler.DisplayFileName == gv.DataHandler.DisplayFileName)
                {
                    dataHandler.FilenameSuffix++;
                    goto restart;
                }
            }
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            TabItem tabPageToClose = (TabItem)tabControlData.SelectedItem;

            //first check that this dataset/datahandler is not in use by any of the analysis pages...
            AnalysesPage analPage = ((App)Application.Current).AnalysesPage;

            List<TabItem> analysesUsedByData = new List<TabItem>();

            if (analPage != null) //..but only if there are analyses present
            {
                GridView gv = (GridView)tabPageToClose.Content;
                DataHandler gridDataHandler = gv.DataHandler;

                foreach (TabItem tab in analPage.tabControlAnalyses.Items)
                {
                    AnalysisView av = (AnalysisView)tab.Content;
                    DataHandler analysisDataHandler = av.Analysis.DataHandler;

                    if (analysisDataHandler == gridDataHandler) //then data is still in use, so add it to the list
                    {
                        analysesUsedByData.Add(tab);
                    }
                }
            }

            bool isClosing = true;
            if (analysesUsedByData.Count > 0) //if is not in use then close
            {
                MessageBoxResult result = MessageBox.Show("This dataset is still in use on the analyses page. Are you sure that you want to close it (any associated analyses will be removed)?", "Data In Use", MessageBoxButton.OKCancel);

                if (result == MessageBoxResult.Cancel) //then close data and associated analyses
                {
                    isClosing = false;
                }
            }

            if (isClosing) //do all the removal stuff
            {
                if (analPage != null)
                {
                    foreach (TabItem tab in analysesUsedByData)
                    {
                        analPage.tabControlAnalyses.Items.Remove(tab);
                    }

                    analPage.tabControlAnalyses.Visibility = SetTabVisible(analPage.tabControlAnalyses.Items.Count > 0);
                }

                tabControlData.Items.Remove(tabPageToClose);

                SetButtonAndTabStatus();

                //update all the datahandlers on any open analysis pages
                if (analPage != null)
                {
                    foreach (TabItem tab in analPage.tabControlAnalyses.Items)
                    {
                        AnalysisView av = (AnalysisView)tab.Content;
                        av.Analysis.UpdateDataHandlers();
                    }
                }
            }
        }

        private void Page_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            //resize the tab control
            double newHeight = e.NewSize.Height - 30;

            if (newHeight > 0)
                tabControlData.Height = newHeight;
            else
                tabControlData.Height = 0;
        }

        private bool firstLoad = true;
        private void StackPanel_Loaded(object sender, RoutedEventArgs e)
        {
            if (firstLoad)
            {
                textAnimation.Begin();
                firstLoad = false;
            }
        }
    }
}